﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace ForkCarRelationManager.db
{
    public class DataPaging
    {
        dbOperate dbo = new dbOperate();
        private DataSet dataset;//数据缓存
        private int count = 0;//总数据量
        private string tablename = "";//数据分页表名
        private int TotalPage = 0;//总页数
        private int OnePageCount = 20;//每页数据
        private int Page = 1;//当前页码
        private int StartId = 1;//开始id
        private int EndId = 1;//结束id
        private string field = "*";//显示字段
        private string condition = "";//数据获取的条件
        private string ordertable = "";
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="tablename">分页数据表表名</param>
        public DataPaging(string tablename)
        {
            this.tablename = tablename;
            SetTotalPage();
        }

        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="tablename">分页数据表表名</param>
        /// <param name="OnePageCount">每页数据条数</param>
        public DataPaging(string tablename, int OnePageCount)
        {
            this.tablename = tablename;
            this.OnePageCount = OnePageCount;
            SetTotalPage();
        }

        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="tablename">分页数据表表名</param>
        /// <param name="OnePageCount">每页数据条数</param>
        /// <param name="field">分页数据显示字段</param>
        public DataPaging(string tablename, int OnePageCount,string field)
        {
            this.tablename = tablename;
            this.OnePageCount = OnePageCount;
            this.field = field;
            SetTotalPage();
        }

        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="tablename">分页数据表表名</param>
        /// <param name="OnePageCount">每页数据条数</param>
        /// <param name="field">分页数据显示字段</param>
        /// <param name="where">补充条件</param>
        public DataPaging(string tablename, int OnePageCount, string field, string condition)
        {
            this.condition = condition;
            this.tablename = tablename;
            this.OnePageCount = OnePageCount;
            this.field = field;
            SetTotalPage();
        }
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="tablename">分页数据表表名</param>
        /// <param name="OnePageCount">每页数据条数</param>
        /// <param name="field">分页数据显示字段</param>
        /// <param name="where">补充条件</param>
        /// <param name="order">根据特定表id</param>
        public DataPaging(string tablename, int OnePageCount, string field, string condition,string ordertable)
        {
            this.condition = condition;
            this.tablename = tablename;
            this.OnePageCount = OnePageCount;
            this.field = field;
            this.ordertable = ordertable;
            SetTotalPage();
        }
        //重载构造函数（用于修复有重复行时的bug）
        public DataPaging(string tablename, int OnePageCount, string field, string condition, string ordertable,bool ifrepeat)
        {
            if (ifrepeat)
            {
                this.condition = condition;
                this.tablename = tablename;
                this.OnePageCount = OnePageCount;
                this.field = field;
                this.ordertable = ordertable;
                setRepeatCount();
            }
            else
            {
                this.condition = condition;
                this.tablename = tablename;
                this.OnePageCount = OnePageCount;
                this.field = field;
                this.ordertable = ordertable;
                SetTotalPage();
            }
           
        }
        /// <summary>
        /// 数据初始化
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public DataSet InitGetData()
        {
            DataSet dataset =null;//= new DataSet();
            if (this.condition.Equals(""))
            {
                if (this.ordertable.Equals(""))
                {
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " order by id"); 
                }
                else
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " order by " + ordertable + ".id"); 

            }
            else
            {
                if (this.ordertable.Equals(""))
                {
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + this.condition + " order by id");
                }
                else
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id");
                string sql = "select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id";
               
            }
            if (dataset != null && dataset.Tables["data"].Rows.Count > 0)
            {
                StartId = Convert.ToInt32(dataset.Tables["data"].Rows[0]["Id"]);
                EndId = Convert.ToInt32(dataset.Tables["data"].Rows[dataset.Tables["data"].Rows.Count - 1]["Id"]);
            }
            return dataset;
        }
       
       
        /// <summary>
        /// 获取下一页数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetFollowData()
        {
            if (Page < TotalPage)
            {
                Page++;
                DataSet dataset = new DataSet();
                if (this.condition.Equals(""))
                {
                    if (this.ordertable.Equals(""))
                    {
                        dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where Id>" + EndId + " order by id");
                    }
                    else
                        dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".Id>" + EndId + " order by " + ordertable + ".id");
                }
                else
                {
                    if (this.ordertable.Equals(""))
                    {
                        dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where Id>" + EndId + " and " + this.condition + " order by id");
                    }
                   else
                        dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".Id>" + EndId + " and " + this.condition + " order by " + ordertable + ".id");
                }
                if (dataset != null && dataset.Tables["data"].Rows.Count > 0)//如果返回数据不为空
                {
                    StartId = Convert.ToInt32(dataset.Tables["data"].Rows[0]["Id"]);
                    EndId = Convert.ToInt32(dataset.Tables["data"].Rows[dataset.Tables["data"].Rows.Count - 1]["Id"]);
                    return dataset;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取上一页数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetForeData()
        {
            if (Page > 1)
            {
                Page--;

                DataSet dataset = new DataSet();
                DataSet tempset = new DataSet();//数据临时缓存

                //倒序数据缓存
                if (this.condition.Equals(""))
                {
                    if (this.ordertable.Equals(""))
                    {
                        tempset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where Id<" + StartId + " order by Id desc");
                    }
                    else
                        tempset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".Id<" + StartId + " order by " + ordertable + ".Id desc");
                }
                else
                {
                    if (this.ordertable.Equals(""))
                    {
                        tempset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where Id<" + StartId + " and " + this.condition + " order by Id desc");
                    }
                    else
                        tempset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".Id<" + StartId + " and " + this.condition + " order by " + ordertable + ".Id desc");
                }
                dataset = dbo.GetDataSet("select DISTINCT " + this.field + " from " + this.tablename + " where 0=1");//获取数据表结构

                if (tempset != null && tempset.Tables["data"].Rows.Count > 0)//如果返回数据不为空
                {
                    int tempsetcount = tempset.Tables["data"].Rows.Count;
                    for (int i = tempsetcount - 1; i >= 0; i--)//将数据倒转
                    {
                        dataset.Tables["data"].Rows.Add(tempset.Tables["data"].Rows[i].ItemArray);
                    }

                    StartId = Convert.ToInt32(dataset.Tables["data"].Rows[0]["Id"]);
                    EndId = Convert.ToInt32(dataset.Tables["data"].Rows[dataset.Tables["data"].Rows.Count - 1]["Id"]);
                    return dataset;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 当前数据缓存刷新
        /// </summary>
        /// <returns></returns>
        public DataSet RefreshData()
        {
            DataSet dataset = new DataSet();
            if (this.condition.Equals(""))
            {
                dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where Id>=" + StartId + " order by id");
            }
            else
            {
                string sql = "select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where Id=" + StartId;// +" and " + this.condition + " order by id";
                dataset = dbo.GetDataSet(sql);
            }
            if (dataset != null && dataset.Tables["data"].Rows.Count > 0)//如果返回数据不为空
            {
                return dataset;
            }
            else
            {
                return null;
            }
        }

        //设置总页数（带重复的）

        public void setRepeatCount()
        {
           count= allData().Rows.Count;
           if (count < OnePageCount)//总数据量小于单页数据
           {
               TotalPage = 1;
           }
           else
           {
               if (count % OnePageCount == 0)
               {
                   TotalPage = count / OnePageCount;
               }
               else
               {
                   TotalPage = count / OnePageCount + 1;
               }
           }
        }
        //设置总页数
        public void SetTotalPage()
        {
            if (this.condition.Equals(""))
            {
                count = dbo.GetCount(tablename, "");
            }
            else
            {
                count = dbo.GetCount(tablename, "where " + this.condition);
            }
            if (count < OnePageCount)//总数据量小于单页数据
            {
                TotalPage = 1;
            }
            else
            {
                if (count % OnePageCount == 0)
                {
                    TotalPage = count / OnePageCount;
                }
                else
                {
                    TotalPage = count / OnePageCount + 1;
                }
            }
        }
        //首页
        public DataSet Homepage()
        {
            DataSet dataset = null;//= new DataSet();
            if (this.condition.Equals(""))
            {
                if (this.ordertable.Equals(""))
                {
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " order by id");
                }
                else
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " order by " + ordertable + ".id");

            }
            else
            {
                if (this.ordertable.Equals(""))
                {
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + this.condition + " order by id");
                }
                else
                    dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id");
                string sql = "select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id";
               
            }
            if (dataset != null && dataset.Tables["data"].Rows.Count > 0)
            {
                StartId = Convert.ToInt32(dataset.Tables["data"].Rows[0]["Id"]);
                EndId = Convert.ToInt32(dataset.Tables["data"].Rows[dataset.Tables["data"].Rows.Count - 1]["Id"]);
            }
            Page = 1;
            return dataset;
        }
        //跳页
        public DataSet SkipPage(int skippage)
        {
            DataSet dataset = null;
            if (skippage > TotalPage||skippage<=0)
            {
                MessageBox.Show("跳转的页面超出范围");
              
            }
            else
            {
                /*int irestPage = count / OnePageCount;
                double drestPage=(double)count/(double)OnePageCount;
                double rest=drestPage-irestPage;*/

                dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".id not in (select top " + ((skippage - 1) * OnePageCount) + " " + ordertable + ".id from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id) and " + this.condition + " order by " + this.ordertable + ".id");
                       
                    
                   
               
                if (dataset != null && dataset.Tables["data"].Rows.Count > 0)
                {
                    StartId = Convert.ToInt32(dataset.Tables["data"].Rows[0]["Id"]);
                    EndId = Convert.ToInt32(dataset.Tables["data"].Rows[dataset.Tables["data"].Rows.Count - 1]["Id"]);
                }

                Page = skippage;
            }

            
            return dataset;
        }
        //末页
        public DataSet TailPage()
        {
            DataSet dataset = null;//= new DataSet();
            int irestPage = count / OnePageCount;
            double drestPage = (double)count / (double)OnePageCount;
            double rest = drestPage - irestPage;
            if (rest==0)
            {
                dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".id not in (select top " + ((irestPage - 1) * OnePageCount) + " " + ordertable + ".id from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id) and " + this.condition + " order by " + this.ordertable + ".id");

            }
            else
            {
                dataset = dbo.GetDataSet("select DISTINCT top " + OnePageCount + " " + this.field + " from " + this.tablename + " where " + ordertable + ".id not in (select top " + (irestPage * OnePageCount) + " " + ordertable + ".id from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id) and " + this.condition + " order by " + this.ordertable + ".id");
            }

            if (dataset != null && dataset.Tables["data"].Rows.Count > 0)
            {
                StartId = Convert.ToInt32(dataset.Tables["data"].Rows[0]["Id"]);
                EndId = Convert.ToInt32(dataset.Tables["data"].Rows[dataset.Tables["data"].Rows.Count - 1]["Id"]);
            }

           
            Page = TotalPage;
            return dataset;
        }
        //获取所有数据
        public DataTable allData()
        {
            DataTable datatable = null;//= new DataSet();
            if (this.condition.Equals(""))
            {
                if (this.ordertable.Equals(""))
                {
                    datatable = dbo.GetDate("select DISTINCT " + this.field + " from " + this.tablename + " order by id");
                }
                else
                    datatable = dbo.GetDate("select DISTINCT  "+ this.field + " from " + this.tablename + " order by " + ordertable + ".id");

            }
            else
            {
                if (this.ordertable.Equals(""))
                {
                    datatable = dbo.GetDate("select DISTINCT " + this.field + " from " + this.tablename + " where " + this.condition + " order by id");
                }
                else
                    datatable = dbo.GetDate("select DISTINCT " + this.field + " from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id");
                string sql = "select DISTINCT " + this.field + " from " + this.tablename + " where " + this.condition + " order by " + ordertable + ".id";

            }
            return datatable;
        }
        public int Getcount()
        {
           
            return count;
        }
        //获取总页数
        public int GetTotalCount()
        {
            return TotalPage;
        }

        //获得当前页码
        public int GetPage()
        {
            return Page;
        }

        //获取StartId
        public int GetStartId()
        {
            return StartId;
        }

        //设置StartId
        public void SetStartId(int id)
        {
            StartId = id;
        }

        //获取EndId
        public int GetEndId()
        {
            return EndId;
        }

        //设置StartId
        public void SetEndId(int id)
        {
            EndId = id;
        }
    }
}
